package cn.cnhis.online.customerView;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import org.json.JSONException;
import org.json.JSONObject;

import cn.cnhis.online.BuildConfig;
import cn.cnhis.online.R;
import cn.cnhis.online.mymodule.Utils.ScreenUtils;


public class AppUpdateDialog extends AlertDialog implements View.OnClickListener {

    private TextView txtYes;
    private TextView txtNo;
    private TextView txtContent;
    private TextView txtTitle;
    private Context context;
    private JSONObject jsonObject;
    private Window window;

    public AppUpdateDialog(Context context, JSONObject jsonObject) {
        super(context);
        this.context = context;
        this.jsonObject = jsonObject;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        window = this.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = ScreenUtils.getScreenWidth(context) - 200;
        window.setAttributes(attributes);
        window.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.circle_box_oval));
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.dialog_update, null);
        txtYes = (TextView) view.findViewById(R.id.txt_yes);
        txtNo = (TextView) view.findViewById(R.id.txt_no);
        txtTitle = (TextView) view.findViewById(R.id.txt_title);
        txtContent = (TextView) view.findViewById(R.id.txt_content);
        txtYes.setOnClickListener(this);
        txtNo.setOnClickListener(this);

        if (BuildConfig.DEBUG) Log.d("AppUpdateDialog", jsonObject.toString());
        try {
            txtTitle.setText(jsonObject.getString("title"));
            txtContent.setText(jsonObject.getString("content"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        this.setCanceledOnTouchOutside(false);
        this.setContentView(view);


    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.txt_no) {
            onUpdateDialogListener.onNo();
        }
        if (v.getId() == R.id.txt_yes) {
            onUpdateDialogListener.onYes();
        }

    }

    OnUpdateDialogListener onUpdateDialogListener;

    public OnUpdateDialogListener getOnUpdateDialogListener() {
        return onUpdateDialogListener;
    }

    public void setOnUpdateDialogListener(OnUpdateDialogListener onUpdateDialogListener) {
        this.onUpdateDialogListener = onUpdateDialogListener;
    }

    public interface OnUpdateDialogListener {
        void onNo();

        void onYes();
    }
}
